﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ircclient
{
    class Privat
    {
        protected string name;
        protected FormMain owner;
        protected FormChat form;
        protected Server server;
        protected string topic;

        public string Topic
        {
            set
            {
                topic = value;
                form.SetTopicBox(value);
            }
            get { return topic; }
        }

        public string Name
        {
            get { return name; }
        }

        public Privat(string name, FormMain owner, Server server)
        {
            this.name = name;
            this.owner = owner;
            owner.Invoke(new Action<string>((s) => CreateForm(s)), name);
            form.RaiseCustomEvent += new EventHandler<FormChat.CustomEventArgs>(form_RaiseCustomEvent);
            server.onMessage += new EventHandler<Server.MessageArg>(server_onMessage);
            this.server = server;
            Topic = name;
            
        }

        protected void server_onMessage(object sender, Server.MessageArg e)
        {
            string SourceNick = e.Source.Split('!')[0];
            if (String.Compare(SourceNick, name, true) == 0)
            {
                if (e.Type == typeMessage.privat)
                    OutMessage(SourceNick, e.Message);
                else if (e.Type == typeMessage.privatnotice)
                    OutNotice(SourceNick, e.Message);
                else if (e.Type == typeMessage.ctcp || e.Type == typeMessage.ctcpnotice)
                    OutNotice(SourceNick, e.Message);
            }
        }

        public void OutMessage(string nick, string text)
        {
           // text = text.Replace(":)", "<img src=file://d:/ab.gif >");
            text = Smiles.Parsing(text);
            OutChan("<span class=\"nick\">&#60;" + nick + "&#62;</span>"  + text);

        }
        public void OutNotice(string nick, string text)
        {
            OutChan("<span class=\"nick\">-&#60;" + nick + "&#62;-</span>" + text);
        }

        public void Close()
        {
            form.Close();
        }

        protected void CreateForm(string text)
        {
            form = new FormChat();
            // Set the Parent Form of the Child window.
            form.MdiParent = owner;
            // Display the new form.
            form.Show();
            form.Text = text;
            form.FormClosing += new FormClosingEventHandler(chanClosing);

        }


        protected void chanClosing(object sender, FormClosingEventArgs e)
        {
            string result = "PART " + name;
            SendServerEvent(new CustomEventArgs(result));

        }

        public class CustomEventArgs : EventArgs
        {
            public CustomEventArgs(string s)
            {
                message = s;
            }
            private string message;

            public string Message
            {
                get { return message; }
                set { message = value; }
            }
        }
        public void OutChan(string text)
        {
            form.AddTextToChat(text + "<br>");
        }

        // Declare the event using EventHandler<T>
        public event EventHandler<CustomEventArgs> RaiseCustomEvent;

        protected FormClosingEventHandler ChanClosing;

        protected virtual void SendServerEvent(CustomEventArgs e)
        {
            EventHandler<CustomEventArgs> handler = RaiseCustomEvent;

            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected void form_RaiseCustomEvent(object sender, FormChat.CustomEventArgs e)
        {
            string result = string.Format("PRIVMSG {0} :{1}", name, e.Message);
            OutChan(result);
            SendServerEvent(new CustomEventArgs(result));
        }
    }
}
